$(function() {
	admin.shop.init();
});
Namespace('admin.shop', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
				/*var row = $('#datagrid').datagrid('getSelected');
				admin.shop.edit(row.id);*/
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	toAdd : function() {
		//top.addTab('#tabs','添加店铺', contextPath+'/mall/shop/add');
	},
	edit:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		top.addTab(id,'店铺编辑', contextPath+'/mall/shop/update/'+id);
	},
	detail:function(id) {
		if(id==null) {
			top.showInfo('请选择一条记录!');
			return;
		}
		top.addTab(id,'店铺查看', contextPath+'/mall/shop/detail/'+id);
	},
	setAuditStatus: function (id,auditStatus) {
		$.messager.confirm('信息提示', "确定要操作吗", function (success) {
			if (!success) {
				return;
			}
			var url = contextPath + '/mall/shop/auditNormal';
			var auditStatusStr = '已通过';
			if (auditStatus == 3) {//不通过
				url = contextPath + '/mall/shop/auditNotpass';
				auditStatusStr = '不通过';
			}
			$.postJson(url, {'id': id}, function (result) {
				if (result.code == 1) {
					top.showInfo('操作成功');
					$('#auditStatusStr').html(auditStatusStr);
					$('#auditAtStr').html(result.map.entity.strAuditAt);
				} else {
					top.showInfo(result.msg);
				}
			});
		});
	},
	setAuditStatus4List: function (id,auditStatus) {
		$.messager.confirm('信息提示', "确定要操作吗", function (success) {
			if (!success) {
				return;
			}
			var url = contextPath + '/mall/shop/auditNormal';
			var auditStatusStr = '已通过';
			if (auditStatus == 3) {//不通过
				url = contextPath + '/mall/shop/auditNotpass';
				auditStatusStr = '不通过';
			}
			$.postJson(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
	                admin.shop.query();
				} else {
					top.showInfo(result.msg);
				}
			});
		});
	},
	setHasRecommend: function (id,hasRecommend) {
		var url = contextPath + '/mall/shop/hasRecommend';
		if (hasRecommend == 1) {
			hasRecommend = 2;
		} else if (hasRecommend == 2) {
			hasRecommend = 1;
		}
		$.postJson(url, {'id': id,'hasRecommend':hasRecommend}, function (result) {
			if (result.code == 1) {
				top.showInfo('操作成功');
				var data = $('#query-form').form('jsonObject');
				$('#datagrid').datagrid('load', data);
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	chooseCategory:function() { //选择类目
		getDialog('query-dialog').dialog({
			href:contextPath+'/mall/shop/category/choose',
			title: '点击选择类目',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.shop.addCategory();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addCategory:function() {
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		if(rows.length > 1){  
			alert("只能选择一行数据!");  
			return false; 
		}
		var categoryId = rows[0].categoryId;
		var categoryName = rows[0].name;
		$('#categoryId').val(categoryId);
		$('#categoryName').val(categoryName);
	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/mall/shop/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
	},
	community:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		top.addTab(id,'店铺社区编辑', contextPath+'/mall/shop/community/edit/'+id);
	},
	setUseQuanPay: function (id) {//启用-圈币支付
		var url = contextPath + '/mall/shop/setUseQuanPay';
		$.post(url, {'id': id}, function (result) {
			if (result.code == 1) {
				top.showInfo('操作成功');
				var data = $('#query-form').form('jsonObject');
				$('#datagrid').datagrid('load', data);
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	setNoUseQuanPay: function (id) {//禁用-圈币支付
		var url = contextPath + '/mall/shop/setNoUseQuanPay';
		$.post(url, {'id': id}, function (result) {
			if (result.code == 1) {
				top.showInfo('操作成功');
				var data = $('#query-form').form('jsonObject');
				$('#datagrid').datagrid('load', data);
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	chooseExtCommunity:function() { //选择服务社区
		getDialog('query-dialog').dialog({
			href:contextPath+'/community/base/choose',
			title: '点击选择服务社区',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.shop.addExtCommunity();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addExtCommunity:function() { //选择服务社区
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		
		var ids = [];
		var names = [];
		for(var i in rows){
			ids.push(rows[i].id);
			names.push(rows[i].name);
		}
		$('#extCommunityIds').val(ids.join(';'));
		$('#extCommunityNames').val(names.join(','));
	},
	setNotValid: function (id) {//设为无效
		var url = contextPath + '/mall/shop/setNotValid';
		$.post(url, {'id': id}, function (result) {
			if (result.code == 1) {
				top.showInfo('操作成功');
				var data = $('#query-form').form('jsonObject');
				$('#datagrid').datagrid('load', data);
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	setIsValid: function (id) {//设为有效
		var url = contextPath + '/mall/shop/setIsValid';
		$.post(url, {'id': id}, function (result) {
			if (result.code == 1) {
				top.showInfo('操作成功');
				var data = $('#query-form').form('jsonObject');
				$('#datagrid').datagrid('load', data);
			} else {
				top.showInfo(result.msg);
			}
		});
	}
});